<?php
/*
 +-------------------------------------------------------------------+
 |                          html.php                                 |
 |                                                                   |
 | Copyright ?                          www.gde.sclab.clarkson.edu   |
 | Author: Demetrios Dimatos            www.dimatos.net              |
 | Created: August 2006                 Last modified: Oct 15, 2006  |
 +-------------------------------------------------------------------+
 | These functions are used throught the GDE project to print query  |
 | query results, print messages, format queries, etc                |
 +-------------------------------------------------------------------+
*/

//Include configuration class
include_once('/var/www/gde-1.0/htdocs/config.php');
// Include templating for paging html 
include_once(Config::base_path.Config::adodb_path.'adodb-pager.inc.php');
// Include templating for rs2html 
include_once(Config::base_path.Config::adodb_path.'tohtml.inc.php');
  
//Example usage for Query2Html, record2Html, record2PagingHtml
//$db=db_connect();
//$sql = 'select * from forum_users ';
//$result = $db->Execute($sql);
//query2Html($result); || record2Html($result); || record2PagingHtml($db,$sql,2);

/**
 * Print a record set from a query to plain html with no formatting
 *
 * @param ADORecordSet object $query_result - SQL statement successfully 
 * is executed by ADOConnection->Execute($sql)
 * @author Demetrios Dimatos
 */
function query2Html($query_result)
{
	//$result = $db->Execute("select * from forum_users");
	if ($query_result == false)
	{
		die("failed");  
	}
	
	while (!$query_result->EOF) 
	{
		for ($i=0, $max=$query_result->FieldCount(); $i < $max; $i++)
		{
		   print $query_result->fields[$i].' ';
		}
		$query_result->MoveNext();
		print "<br>\n";
	}
}

/**
 * Print a record set from a query to formatted html in a talble
 *
 * @param ADORecordSet object $query_result - Executed SQL successful statement 
 * @param string $name - title used for the table in the first row
 * @author Demetrios Dimatos
 */
function record2Html($query_result,$name="")
{
	if($name != "") // No name is passed
	{
		rs2html($query_result,' border=1 cellpadding=2 cellspacing=2',array($name));//,array('Customer Name','Customer ID')); 
		echo "<br>";
	}
	else						// Use default field name
	{
		rs2html($query_result,' border=1 cellpadding=2 cellspacing=2');
		echo "<br>";
	}
}

/**
 *  Print record sets with paginating into a formatted table
 *
 * @param connection $database_connection - Non-persistent connect to data source or server
 * @param string $sql - SQL statement
 * @param ineteger $rows_per_page - number of rows to pagenate on
 * @author Demetrios Dimatos
 */
function record2PagingHtml($database_connection,$sql,$rows_per_page)
{
	//$sql = 'select * from forum_users where username="'.Config::db_username.'"';
	$pager = new ADODB_Pager($database_connection,$sql);
	$pager->Render($rows_per_page);
}

/**
 * Ehco's a message in chosen color to an html page
 *
 * @param string $mesg - message to output
 * @param string $color - color of message red,black, etc..
 * @param unknown_type $align - alignment of message, left , center, etc.
 * @author Demetrios Dimatos
 */
function message2html($mesg, $color,$align="left")
{
	echo '<br><b><div style="text-align: '.$align.'; font color="'.$color.'">'.$mesg.'</div><br></b>';
}

//Function inserts generates an html page with a url and message
/**
 * Echo's a message to html that is a hyperlink
 *
 * @param string $mesg - message to output
 * @param string $url - desired hyperlink for the message
 * @author Demetrios Dimatos
 */
function message2url($mesg, $url)
{
	echo '<html><head><body><a href="'.$url.'" class="body" title="Click here to login.">'.$mesg.'</a></body></html>';
}

/**
 * Echo's a message to an html page and then redirects on time entered
 *
 * @param string $mesg - message to output
 * @param string $redirect - url to redirect too
 * @param integer $time - time before page is redirected
 * @author Demetrios Dimatos
 */
function message2redirect($mesg, $redirect, $time)
{
	echo '<html><head><META HTTP-EQUIV="Refresh" CONTENT="'.$time.'; URL='.$redirect.'"> </head>
	<body>'.$mesg.'</body></html>';
}

/**
 * Displays database description and a hyperlink then sends this data to the url for a GET method
 *
 * @param ADORecordSet object $result - Executed SQL successful statement 
 * @author Demetrios Dimatos
 */
function databaseDescription2url($result)
{
	echo '<table style="text-align: left; width: 80%;" border="1" cellpadding="2" cellspacing="2">';
	while (!$result->EOF)
	{
		echo '<tr><td>';
		echo '<a href="'.$_SERVER['PHP_SELF'].'?database='.$result->fields[0].'" class="body" title="'.$result->fields[1].'">'.$result->fields[1].'</a></td><td>';
  	echo $result->fields[2];
  	echo "</td></tr>";
  	$result->MoveNext();
	}
	echo '</table>';
}

/**
 * Displays table descriptions and a hyperlink then sends this data to the url for a GET method
 *
 * @param ADORecordSet object $result - Executed SQL from getTableNames()
 * @param integer $databaseID - Database ID returned from the GET in databaseDescription2url()
 * @param ADORecordSet object $table_Descriptions - Executed SQL from getTablesDescription()
 * @author Demetrios Dimatos
 */
function tableDescription2url($result,$databaseID,$table_Descriptions)
{ 
	$i = 0;
	echo '<table style="text-align: left; width: 80%;" border="1" cellpadding="2" cellspacing="2">';
	while (!$result->EOF)
	{
		echo '<tr><td>';
		echo '<a href="'.$_SERVER['PHP_SELF'].'?database='.$databaseID.'&table='.$result->fields[0].'"class="body" title="'.$result->fields[0].'">'.$result->fields[0].'</a></td><td>';
  	echo $table_Descriptions[$i];
  	echo "</td></tr>";
  	$i += 1;
  	$result->MoveNext();
	}
	echo '</table>';
}

//Function genterates Database Descriptions hyperlinks for user to choose
/**
 * Displays field descriptions and a hyperlink then sends this data to the url for a GET method
 *
 * @param ADORecordSet object $result - Executed SQL from getTableNames()
 * @param integer $databaseID - Database ID returned from the GET in databaseDescription2url()
 * @param unknown_type $table - Table returned from the GET method by tableDescription2url()
 * @author Demetrios Dimatos
 */
function fieldDescription2url($result,$databaseID,$table)
{ 
	$desc = array();
	echo '<table style="text-align: left; width: 80%;" border="1" cellpadding="2" cellspacing="2">';

	while(!$result->EOF)
	{
		echo '<tr><td>';
		echo '<a href="'.$_SERVER['PHP_SELF'].'?database='.$databaseID.'&table='.$table.'&field='.$result->fields[0].'"class="body" title="'.$result->fields[0].'">'.$result->fields[0].'</a>';
  	echo "</td></tr>";
  	$result->MoveNext();
	}
	echo '</table>';
}
/**
 * nRecord2Html - Formats up to 12 arrays data(same size) into an html table
 * Exmaple: Record2Html(myArray1,myArray2,myArray3,"","","","","","","","","",3);
 * @param array $arr1 - data
 * @param array $arr2 - data
 * @param array $arr3 - data
 * @param array $arr4 - data
 * @param array $arr5 - data
 * @param array $arr6 - data
 * @param array $arr7 - data
 * @param array $arr8 - data
 * @param array $arr9 - data
 * @param array $arr10 - data
 * @param array $arr11 - data
 * @param array $arr12 - data
 * @param int $num_arrays - number of arrays not null
 * 
 * @author Demetrios Dimatos
 */
function nRecord2Html($arr1,$arr2,$arr3,$arr4,$arr5,$arr6,$arr7,$arr8,$arr9,$arr10,$arr11,$arr12,$num_arrays)
{
	$num_arr_values = 0;  // Number of arrays not empty
	
	// Set up 2 Dimensional arry
	$array_n = array($arr1,$arr2,$arr3,$arr4,$arr5,$arr6,$arr7,$arr8,$arr9,$arr10,$arr11,$arr12);
	
	// Determine how many values in any single array
	$num_arr_values = sizeof($arr1);
	
  echo '<table style="text-align: left; width: 80%;" border="1" cellpadding="2" cellspacing="2">';
  
	for($col=0; $col < $num_arr_values ; $col++)
	{
		echo '<tr>';
		// Set up table cells based on how many arrays not null
		for($row=0; $row < $num_arrays ; $row++)
		{
				echo '<td>'.$array_n[$row][$col].'</td>';
		}
		echo '</tr>';
	}
	echo '</table>';
}

/**
 * showGraph - generates and displays a graph with prefferd options
 * This function will display the graph, no return type.
 * @param array $array_keys - keys from histogram array
 * @param array $array_values - values from histogram array
 * @param string $abs_prefix	- prefix used to indicate abs purpose
 * @param string $legend_title - title for legend
 * @author Demetrios Dimatos
 */
function showGraph($array_keys, $array_values,$abs_prefix,$legend_title)
{
	$graph = new BAR_GRAPH("hBar");
 	$graph->labels = $array_keys;
 	$graph->labelSize = 11;
 	$graph->values = $array_values;
 	$graph->showValues =1;
 	$graph->absValuesSize = 11;
 	$graph->absValuesPrefix = $abs_prefix; //"Freq: ";
 	$graph->charts = 2;
 	$graph->legend = $legend_title;//"Item's percentage";
 				
 	echo $graph->create();
}
?>